@echo off

call svn update

set NOCOMMIT=

:Loop
IF "%1"=="" GOTO Continue
if "%1"=="nocommit" set NOCOMMIT=1
SHIFT
GOTO Loop
:Continue


for /f "tokens=1,2 delims= " %%a in ('svn.exe st -q') do (
if "%%a"=="M" echo "%%b" changed
if "%%a"=="M" echo Commit changes before making distrib!
if "%%a"=="M" rem pause
if "%%a"=="M" if not "%NOCOMMIT%"=="1" exit /b 1
if "%%a"=="M" rem goto :end
)

svn info > nul
if errorlevel 1 goto :OnError

for /F "tokens=1,2 delims=: " %%a in ('svn info') do call :InfoProc "%%a" "%%b"
goto :end

:InfoProc
if %1=="Revision" call :WriteRevision %2
goto :end

:WriteRevision
echo #ifndef VIEW_REVISION_H > revision.h
echo #define REVISION_NUM %~1 >> revision.h
call :WriteBody
goto :end

:OnError
echo Error get revision from SVN
if not "%1"=="WriteFakeHdrOnError" exit /b 1
if exist revision.h goto end
echo generating fake header

echo #ifndef VIEW_REVISION_H > revision.h
echo #define REVISION_NUM 0 >> revision.h
call :WriteBody
goto end

:WriteBody
echo. >> revision.h
echo // Don't care about follows >> revision.h
echo. >> revision.h
echo // Macros utils >> revision.h
echo #define REV_STRINGIZE1(a) #a >> revision.h
echo #define REV_STRINGIZE(a) REV_STRINGIZE1(a) >> revision.h
echo #define REV_LONGIZE1(a) L##a >> revision.h
echo #define REV_LONGIZE(a) REV_LONGIZE1(a) >> revision.h
echo. >> revision.h
echo // Auto definitions >> revision.h
echo #define A_REVISION_NUM_STR  REV_STRINGIZE(REVISION_NUM) >> revision.h
echo #define L_REVISION_NUM_STR  REV_LONGIZE(A_REVISION_NUM_STR) >> revision.h
echo. >> revision.h
echo #define VIEW_REVISION_H >> revision.h
echo #endif >> revision.h
echo. >> revision.h
goto :end
:end